package com.experienceBaby.Cashbook.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.experienceBaby.Cashbook.R;
import com.experienceBaby.Cashbook.databinding.ActivityMainBinding;
import com.experienceBaby.Cashbook.fragment.AddFragment;
import com.experienceBaby.Cashbook.fragment.ChartFragment;
import com.experienceBaby.Cashbook.fragment.DetailFragment;
import com.experienceBaby.Cashbook.fragment.UserFragment;
import com.experienceBaby.Cashbook.greendao.DbController;
import com.google.android.material.tabs.TabLayout;
import com.xuexiang.xui.XUI;
import com.xuexiang.xui.utils.WidgetUtils;

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding mBinding;
    private DbController mDbController;

    private FragmentManager supportFragmentManager;

    /**
     * 0：详情
     * 1：图表
     * 2：我的
     * 3：添加
     */
    private int status;
    private boolean isAdd = false;

    private long exitTime = 0;

    private AddFragment addFragment;
    private ChartFragment chartFragment;
    private DetailFragment detailFragment;
    private UserFragment userFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        XUI.init(getApplication());
        XUI.initTheme(this);
        super.onCreate(savedInstanceState);
        // View Binding
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        View rootView = mBinding.getRoot();
        setContentView(rootView);

        mDbController = DbController.getInstance(this);

        initData();

        initViews();
    }

    private void initViews() {
        supportFragmentManager = getSupportFragmentManager();
        initTab();
        initFAB();
    }

    /**
     * 悬浮按钮初始化
     */
    private void initFAB() {
        //Activity版本
        mBinding.fab.setOnClickListener(view -> {
            startActivity(new Intent(this, AddActivity.class));
        });
    }

    /**
     * 初始化菜单栏
     * 明细，记账，图表
     */
    private void initTab() {
        TabLayout.Tab detail = mBinding.tabs.newTab();
        detail.setText("明细");
        detail.setIcon(R.drawable.selector_detail);
        mBinding.tabs.addTab(detail);

//        TabLayout.Tab add = mBinding.tabs.newTab();
//        add.setText("添加");
//        add.setIcon(R.drawable.selector_add);
//        mBinding.tabs.addTab(add);

        TabLayout.Tab chart = mBinding.tabs.newTab();
        chart.setText("图表");
        chart.setIcon(R.drawable.selector_chart);
        mBinding.tabs.addTab(chart);

        TabLayout.Tab user = mBinding.tabs.newTab();
        user.setText("我的");
        user.setIcon(R.drawable.selector_user);
        mBinding.tabs.addTab(user);

        status = 0;

        WidgetUtils.setTabLayoutTextFont(mBinding.tabs);

        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        transaction
                .add(R.id.fragment, detailFragment)
                .add(R.id.fragment, addFragment)
                .add(R.id.fragment, chartFragment)
                .add(R.id.fragment, userFragment)
                .show(detailFragment)
                .hide(addFragment)
                .hide(chartFragment)
                .hide(userFragment)
                .commit();

        mBinding.tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                isAdd = false;
                status = tab.getPosition();
                switchPage();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void switchPage() {
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        //获得当前点击的是第几个
        switch (status) {
            case 1:
                transaction
                        .hide(detailFragment)
                        .hide(addFragment)
                        .show(chartFragment)
                        .hide(userFragment);
                break;
            case 2:
                transaction
                        .hide(detailFragment)
                        .hide(addFragment)
                        .hide(chartFragment)
                        .show(userFragment);
                break;
            case 0:
            default:
                transaction
                        .show(detailFragment)
                        .hide(addFragment)
                        .hide(chartFragment)
                        .hide(userFragment);
                break;
        }
        transaction.commit();
    }

    private void initData() {
        initFragment();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isAdd) {
                switchPage();
                isAdd = false;
                return false;
            } else {
                if ((System.currentTimeMillis() - exitTime) > 2000) {
                    showToast("再按一次退出程序");
                    exitTime = System.currentTimeMillis();
                    return false;
                } else {
                    //关闭当前界面窗口
                    finish();
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initFragment() {
        addFragment = new AddFragment();
        chartFragment = new ChartFragment();
        detailFragment = new DetailFragment();
        userFragment = new UserFragment();
    }

    private void showToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBinding = null;
    }
}